package com.controller
{
	
	import com.model.valueObjects.VOStateMaster;
	import com.service.StateMaster;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	
	
	public class StateMasterController extends AbstractController
	{
		import com.model.Model;
		
		[Inject]
		public var stateMasterDetails:StateMaster;
		
		
		//[PostConstruct]
		public function StateMasterController()
		{
			super();	
		}
		
		[EventHandler(event="StateMasterEvent.GET_ALL")]
		public function getAll():void{
			sh.executeServiceCall(stateMasterDetails.getAllStates(),getAllResult, handleFault );
		}
		private function getAllResult(event:ResultEvent):void
		{
			model.stateMasterList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="StateMasterEvent.GET_BY_FK", properties="countryFk")]
		public function getbyFK(countryFk:int):void{
			sh.executeServiceCall(stateMasterDetails.getStatesByFK(countryFk),getByFk, handleFault );
		}
		private function getByFk(event:ResultEvent):void
		{
			model.stateMasterList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
		}
		
		[EventHandler(event="StateMasterEvent.ADD", properties="stateMastervo")]
		public function add(stateMastervo:VOStateMaster):void
		{
			sh.executeServiceCall(stateMasterDetails.addNewState(stateMastervo), addResult, handleFault );	
		}
		
		private function addResult(event:ResultEvent):void
		{
			model.stateMasterList = event.result as ArrayCollection;
			
			if(model.saveCloseBtn)model.currentForm=1;
			
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("New State Added" );
		}
		
		[EventHandler(event="StateMasterEvent.UPDATE", properties="stateMastervo")]
		public function updateState(stateMastervo:VOStateMaster):void
		{
			sh.executeServiceCall(stateMasterDetails.editState(stateMastervo), updateResult, handleFault );	
		}
		private function updateResult(event:ResultEvent):void
		{
			model.stateMasterList = event.result as ArrayCollection;
			model.currentForm=1
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("Details Updated ");
		}
		
		[EventHandler(event="StateMasterEvent.DELETE", properties="stateSerialNo")]
		public function deleteR(stateSerialNo:int):void
		{
			sh.executeServiceCall(stateMasterDetails.deleteState(stateSerialNo), deleteResult, handleFault );	
		}
		private function deleteResult(event:ResultEvent):void
		{
			getAll();
			model.currentForm=1
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("Record Deleted");
		}
		
		private function handleFault(event:FaultEvent):void
		{
			Alert.show( event.fault.faultDetail, "Error" );
		}
	}
}